﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Model;
using University.Workshop.Business;
using System.IO;

namespace University.Workshop.WinApp
{
    public partial class TodayAppointment : Form
    {
        public TodayAppointment()
        {
            InitializeComponent();
            LoadDataAppointmentsForToday();
            IssuePermits();
        }

        private void IssuePermits()
        {
            //buttonMakeTreatment.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        }

        private void buttonMakeTreatment_Click(object sender, EventArgs e)
        {

        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PendingTreatments_Load(object sender, EventArgs e)
        {

        }

        private void dataGridViewAppointmentsForToday_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                LoadDataTreatmentOfTheSelectedAppointment();
                LoadDataPatientsNextAppointment();
            }
            catch (Exception)
            {                
                //throw;
            }
        }

        private void LoadDataTreatmentOfTheSelectedAppointment()
        {
            dataGridViewTreatmentOfTheSelectedAppointment.DataSource = TreatmentManager.LoadDataGridViewTreatmentOfTheSelectedAppointment();
        }

        private void LoadDataPatientsNextAppointment()
        {
            //dataGridViewPatientsNextAppointment.DataSource = AgendaManager.LoadDataPatientsNextAppointment(10);
        }

        private void LoadDataAppointmentsForToday()
        {
            
            DataSet ds = new DataSet();
            ds.Tables.Add(AgendaManager.LoadDataAppointmentsForToday().Copy());
            ds.Tables[0].Columns.Add("PictureBits", typeof(Image));
            ds.Tables[0].Columns.Add("LastName");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                try
                {
                    Image img = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", dr["Picture"])));
                    dr["PictureBits"] = img;  
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message); 
                    Image img = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", "anonimos.jpg")));
                    dr["PictureBits"] = img;
                }
            }
            dataGridViewAppointmentsForToday.AutoGenerateColumns = false;
            dataGridViewAppointmentsForToday.DataSource = ds.Tables[0];            
            
        }
    }
}
